//
//  FixThrowView.m
//  HaoDeCaiFu
//
//  Created by admin on 2017/12/25.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "FixThrowView.h"
#import "FixThrowViewCell.h"
#define ViewHeight 260

static id _publishContent;//类方法中的全局变量这样用（类型前面加static）
//static UIVisualEffectView *_effectView;//毛玻璃效果
static UIView *_effectView;
static UIPageControl * imagePC;//跟随scrollview滚动
static NSArray * array;
static UITableView * tableView;
static NSInteger indexValue;

static NSString * identifier = @"FixThrowViewCell";
@implementation FixThrowView
+(void)shareWithContent:(NSArray *)arr title:(NSString *)title index:(NSInteger)indexs
{
    array = arr;
    indexValue = indexs;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    //毛玻璃特效类型
    //UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    //_effectView = [[UIVisualEffectView alloc]initWithEffect:blur];
    _effectView=[[UIView alloc]init];
    _effectView.frame = CGRectMake(0, 0, screenWidth, screenHeight);
    //设置模糊透明度
    _effectView.alpha = 0.8f;
    [window addSubview:_effectView];
    
    
    /**
     点击退出手势
     */
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [_effectView addGestureRecognizer:tap];
    
    
    /**
     加上一层黑色透明效果
     */
    UIView *blackV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    blackV.backgroundColor = [UIColor grayColor];
    blackV.alpha = 0.5f;
    [_effectView addSubview:blackV];
    
    
    
    UIView *shareView = [[UIView alloc] initWithFrame:CGRectMake(0, screenHeight-ViewHeight, screenWidth, ViewHeight)];
    shareView.tag = 441;
    shareView.backgroundColor = [UIColor whiteColor];
    [window addSubview:shareView];
    
    UILabel * titleLabel = [[UILabel alloc]init];
    titleLabel.frame = CGRectMake(0, 0, screenWidth, 40);
    titleLabel.text = title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:16.0];
    [shareView addSubview:titleLabel];
    
    
    UILabel * lineLabel = [[UILabel alloc]init];
    lineLabel.frame = CGRectMake(0,titleLabel.bottom-1,screenWidth,0.5);
    lineLabel.backgroundColor = RGB(204, 204, 204);
    [shareView addSubview:lineLabel];
    
    
    tableView = [[UITableView alloc]init];
    tableView.frame = CGRectMake(0, titleLabel.bottom, screenWidth, shareView.height - titleLabel.height);
    tableView.dataSource = (id)self;
    tableView.delegate = (id)self;
    //[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"identifierOne"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [tableView registerNib:[UINib nibWithNibName:@"FixThrowViewCell" bundle:nil] forCellReuseIdentifier:identifier];
    tableView.backgroundColor = [UIColor whiteColor];
    [shareView addSubview:tableView];
    
    
    
    //为了弹窗不那么生硬，这里加了个简单的动画
    shareView.frame = CGRectMake(0, screenHeight, screenWidth, ViewHeight);
    //shareView.transform = CGAffineTransformMakeScale(1/300.0f, 1/270.0f);
    //_effectView.contentView.alpha = 0;
    _effectView.alpha = 0;
    [UIView animateWithDuration:0.35f animations:^{
        shareView.frame = CGRectMake(0, screenHeight-ViewHeight, screenWidth, ViewHeight);
        //shareView.transform = CGAffineTransformMakeScale(1, 1);
        //_effectView.contentView.alpha = 1;
        _effectView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}
+ (void)dismiss {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *shareView = [window viewWithTag:441];
    
    //为了弹窗不那么生硬，这里加了个简单的动画
    shareView.transform = CGAffineTransformMakeScale(1, 1);
    [UIView animateWithDuration:0.35f animations:^{
        shareView.frame = CGRectMake(0, screenHeight, screenWidth, ViewHeight);
        //shareView.transform = CGAffineTransformMakeScale(1/300.0f, 1/270.0f);
        //_effectView.contentView.alpha = 0;
        _effectView.alpha = 0;
    } completion:^(BOOL finished) {
        
        [shareView removeFromSuperview];
        [_effectView removeFromSuperview];
        [imagePC removeFromSuperview];
    }];
}
#pragma mark -
#pragma mark UITableViewDelegate  UITableViewDataSource
+(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
+ (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //LLLog(@"%ld",indexPath.row);
    if (clickBlocks != nil) {
        clickBlocks((long)indexPath.row);
    }
    [self dismiss];
}
+ (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return array.count;
}
+ (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FixThrowViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    }
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = array[indexPath.row];
    if (indexValue == indexPath.row) {
        cell.rightImageView.image = [UIImage imageNamed:@"icon_select"];
    }
    else
    {
        cell.rightImageView.image = [UIImage imageNamed:@"noimage"];
    }
    return cell;
}
//+(void)shareImgClick:(UITapGestureRecognizer *)recognizer
//{
//    LLLog(@"点击图片的tag是:%ld",(long)recognizer.view.tag);
//    NSString * URLString = [array[(long)recognizer.view.tag-100] objectForKey:@"URL"];
//    if (URLString.length != 0 && [[array[(long)recognizer.view.tag-100] objectForKey:@"IS_Click"] isEqualToString:@"0"]) {
//        [self dismiss];
//    }
//
//
//    if (clickBlocks != nil) {
//        clickBlocks((long)recognizer.view.tag);
//    }
//}

+(void)returnClickIndexBlock:(ReturnClickIndex)clickBlock
{
    clickBlocks = clickBlock;
}


@end
